#define PI 3.1415
#define lightx 0.5
#define lighty 0.5
#define Threshold 0.1

precision highp float;

//varying vec3 n;
//varying vec2 uv;

uniform sampler2D texture;
vec4 withDistance(vec2 uv, float colorUnitValue);

void main(void)
{
	vec2 uv = gl_TexCoord[0].xy;
	//translate u and v into [-1 , 1] domain
	float u0 = uv.x * 2. - 1.;
	float v0 = uv.y * 2. - 1.;

	//then, as u0 approaches 0 (the center), v should also approach 0
	v0 = v0 * abs(u0);
	//convert back from [-1,1] domain to [0,1] domain
	v0 = (v0 + 1.) / 2.;
	//we now have the coordinates for reading from the initial image
	vec2 newCoords = vec2(uv.x, v0);

	//read for both horizontal and vertical direction and store them in separate channels
	float horizontal = withDistance(newCoords, texture2D(texture, newCoords).a);
	float vertical = withDistance(newCoords, texture2D(texture, newCoords.yx).a);

	gl_FragColor = vec4(horizontal, vertical, 0., 1.);
}

// Computes the distance from the origin
vec4 withDistance(vec2 uv, float colorUnitValue)
{
	float distance = colorUnitValue > 0.3 ? length(uv - 0.5) : 1.;
	return distance;
}